package DBL;
import java.net.URL;
import java.sql.*;
import java.util.*;
/**
 * @Author Kis Boisen Hansen
 * @Version 2006.10.02 - revised marts 2009 FEN
 * This class is used to create the connection to the database
 * It is implementet as a singelton. The constructor is private to encure that only
 * one object of the class is generated
 * Version for Sql Server or MySql lines in comments
 */

public class DBConnection
{
    //constans used to get access to the database
	//SQL Server
 //   private static final String  driver = "jdbc:odbc:";
  	//MySql  mysql05.talkactive.net
    private static final String  driver = "jdbc:mysql://db.n-nohr.dk:3306/";
    private static final String  databaseName = "web412856";
	//SQL Server
    private static final String  userName = "web412856";
    private static final String password = "e638wyj672";
    //MySql password and username
    //private static final String  userName = "root";
    //private static final String password = "fisk";
    private DatabaseMetaData dma;
    private static Connection con;
    // an instance of the class is generetated
    private static DBConnection  instance = null;

    // the constructor is private to ensure that only one object of this class is createt
    private DBConnection()
    {
    	String url = driver + databaseName;
    	try{
            //load af driver
            //SQL Server
          //  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            // mySQL
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch(Exception e){
            System.out.println("Can not find the driver");
            System.out.println(e.getMessage());
        }//end catch
        try{
            //connection to the database
            con = DriverManager.getConnection(url, userName,password);
            //set autocommit
            con.setAutoCommit(true);

            dma = con.getMetaData(); // get meta data
            System.out.println("Connection to " + dma.getURL());
            System.out.println("Driver " + dma.getDriverName());
            System.out.println("Database product name " + dma.getDatabaseProductName());
        }//end try
        catch(Exception e){
            System.out.println("Problems with the connection to the database");
            System.out.println(e.getMessage());
        }//end catch
    }//end  constructor

  //closeDb: closes the connection to the database
    public static void closeConnection()
    {
       	try{
            con.close();
            System.out.println("The connection is closed");
        }
         catch (Exception e){
            System.out.println("Error trying to close the database " +  e.getMessage());
         }
    }//end closeDB

    //getDBcon: Get-metode, returnerer forbindelsen til databasen
    public  Connection getDBcon()
    {
       return con;
    }
    //this method is used to get the instance of the connection
    public static DBConnection getInstance()
    {
        if (instance == null)
        {
          instance = new DBConnection();
        }
        return instance;
    }

}//end DbConnection
